package org.hk.kg.ui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.hk.kg.KGMain;
import org.hk.kg.security.Logon;

public class LogonPanel {
	private JDialog logonWin;
	private final int WIDTH = 400,HEIGHT = 200;
	
	public LogonPanel(JFrame containner)
	{
		logonWin = new JDialog(containner);
		logonWin.setSize(WIDTH, HEIGHT);
		logonWin.setLocationRelativeTo(containner);
		logonWin.setResizable(false);
		logonWin.setAlwaysOnTop(true);
		logonWin.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				System.exit(0);
			}
		});
		
		BoxLayout box = new BoxLayout(logonWin.getContentPane(), BoxLayout.Y_AXIS);
		logonWin.setLayout(box);
		
		initInput();
		logonWin.setVisible(true); 
		logonWin.toFront();
	}
	
	private void initInput()
	{	
		JPanel usr_row = getRow();
		getLabel("用户", usr_row);
		final JTextField usrIpt = new JTextField(10);
		usr_row.add(usrIpt);
		final JLabel status_usrIpt = getLabel("请输入帐号",usr_row);
		
		JPanel password_row = getRow();
		getLabel("密码", password_row);
		final JPasswordField passwordIpt = new JPasswordField(10);
		password_row.add(passwordIpt);
		final JLabel status_passwordIpt = getLabel("请输入密码",password_row);
		
		JPanel checkCode_row = getRow();
		checkCode_row.add(new JButton("获取验证码"));
		final JTextField lclChkCodeIpt = new JTextField(5);
		checkCode_row.add(lclChkCodeIpt);
		final JLabel status_lclChkCodeIpt = getLabel("请将获取的验证码输入手机",checkCode_row);
		
		JPanel moble_row = getRow();
		getLabel("手机验证码", moble_row);
		final JTextField mobileChkCodeIpt = new JTextField(5);
		moble_row.add(mobileChkCodeIpt);
		final JLabel status_mobileChkCodeIpt = getLabel("请填入手机生成的数字",moble_row);
		
		JPanel button_row = getRow();
		button_row.add(createButton("登陆",new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String usr = usrIpt.getText(),
					password = new String(passwordIpt.getPassword()),
					lclChkCode = lclChkCodeIpt.getText(),
					mobileChkCode = mobileChkCodeIpt.getText();
				
				int status = 0;
				if((status = Logon.checkLogin(usr, password, lclChkCode, mobileChkCode)) == Logon.COMMON_SUCCESS)
				{
					KGMain.body.setVisible(true);
					logonWin.dispose();
				}
				else
				{
					if(status == Logon.USER_NOT_EXITS)
					{
						//KGUiContext.instance.error(Constants.COMMON_FAIL_ACTION, Logon.ERROR_USER_NOT_EXITS);
					}
					else if(status == Logon.PASSWORD_ERROR)
					{
						//KGUiContext.instance.error(Constants.COMMON_FAIL_ACTION, Logon.ERROR_PASSWORD_ERROR);
					}
					else if(status == Logon.LOCAL_CODE_ERROR)
					{
						//KGUiContext.instance.error(Constants.COMMON_FAIL_ACTION, Logon.ERROR_LOCAL_CODE_ERROR);
					}
					else if(status == Logon.MOBLIE_CODE_ERROR)
					{
						//KGUiContext.instance.error(Constants.COMMON_FAIL_ACTION, Logon.ERROR_MOBLIE_CODE_ERROR);
					}
				}
			}
		}));
		button_row.add(createButton("取消",new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		}));
	}
	
	private JPanel getRow()
	{
		JPanel p = new JPanel();
		FlowLayout layout = (FlowLayout) p.getLayout();
		layout.setAlignment(FlowLayout.LEFT);
		logonWin.add(p);
		p.add(new JLabel("             "));
		return p;
	}
	
	private JLabel getLabel(String txt,JComponent comp)
	{
		JLabel label = new JLabel(txt);
		comp.add(label);
		return label;
	}
	
	
	private JButton createButton(String txt,ActionListener listener)
	{
		JButton bt = new JButton(txt);
		bt.addActionListener(listener);
		return bt;
	}
}
